﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;


namespace QLTV_V2
{
    public partial class FrmTimKiemAnPham : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmTimKiemAnPham()
        {
            InitializeComponent();
        }
        Utilities uti = new Utilities();
        ConnectData conn = new ConnectData();
        ExportToExcel exp = new ExportToExcel();
        DataTable dtb=null;
        private void FrmTimKiemAnPham_Load(object sender, EventArgs e)
        {
            //Load combobox Loai sach
            DataTable tbLoaiSach = uti.getDataSource("LoaiSach", "TenLoaiSach");
            comb_MaLoaiSach.DataSource = tbLoaiSach;
            DataRow drLoaiSach = tbLoaiSach.NewRow();
            drLoaiSach["MaLoaiSach"] = "all";
            drLoaiSach["TenLoaiSach"] = "Tất cả";
            tbLoaiSach.Rows.InsertAt(drLoaiSach, 0);
            comb_MaLoaiSach.DisplayMember = "TenLoaiSach";
            comb_MaLoaiSach.ValueMember = "MaLoaiSach";
            comb_MaLoaiSach.SelectedIndex = 0;
            //Load combobox Tac gia
            DataTable tbTacGia = uti.getDataSource("TacGia", "TenTacGia");
            comb_MaTacGia.DataSource = tbTacGia;
            DataRow drTacGia = tbTacGia.NewRow();
            drTacGia["MaTacGia"] = "all";
            drTacGia["TenTacGia"] = "Tất cả";
            tbTacGia.Rows.InsertAt(drTacGia, 0);
            comb_MaTacGia.DisplayMember = "TenTacGia";
            comb_MaTacGia.ValueMember = "MaTacGia";
            comb_MaTacGia.SelectedIndex = 0;
            //Load combobox Nha xuat ban
            DataTable tbNXB = uti.getDataSource("NhaXuatBan", "TenNXB");
            comb_MaNXB.DataSource = tbNXB;
            DataRow drNXB = tbNXB.NewRow();
            drNXB["MaNXB"] = "all";
            drNXB["TenNXB"] = "Tất cả";
            tbNXB.Rows.InsertAt(drNXB, 0);
            comb_MaNXB.DisplayMember = "TenNXB";
            comb_MaNXB.ValueMember = "MaNXB";
            comb_MaNXB.SelectedIndex = 0;
            //Load combobox tinh trang
            comb_TinhTrang.Items.Add("Tất cả");
            comb_TinhTrang.Items.Add("Tốt");
            comb_TinhTrang.Items.Add("Hỏng");
            comb_TinhTrang.SelectedIndex = 0;
            //Load combobox trnang thai
            comb_TrangThai.Items.Add("Tất cả");
            comb_TrangThai.Items.Add("Sẵn sàng");
            comb_TrangThai.Items.Add("Cho mượn");
            comb_TrangThai.Items.Add("Thanh lý");
            comb_TrangThai.SelectedIndex = 0;
        }
        string getSql()
        {
            string sql = "select  distinct a.MaDauSach,TenDauSach,count(a.mabansach) as 'SoLuong', TenLoaiSach,TenTacGia, NamXB, TenNXB "+
                         "from BanSach a, DauSach b,LoaiSach c,TacGia d, NhaXuatBan e "+
                         "where a.MaDauSach=b.MaDauSach and b.MaTacGia=d.MaTacGia and b.MaLoaiSach=c.MaLoaiSach and b.MaNXB=e.MaNXB ";
            if (comb_MaLoaiSach.SelectedIndex != 0)
                sql += "and b.MaLoaiSach =N'" + comb_MaLoaiSach.SelectedValue.ToString() + "' ";
            if (comb_MaNXB.SelectedIndex != 0)
                sql += "and b.MaNXB =N'" + comb_MaNXB.SelectedValue.ToString() + "' ";
            if (comb_MaTacGia.SelectedIndex != 0)
                sql += "and b.MaTacGia =N'" + comb_MaTacGia.SelectedValue.ToString() + "' ";
            if (txt_MaDauSach.Text != "")
                sql += "and a.MaDauSach like N'%" + txt_MaDauSach.Text + "%' ";
            if (txt_MaBanSach.Text != "")
                sql += "and MaBanSach like N'%" + txt_MaBanSach.Text + "%' ";
            if (txt_TenDauSach.Text != "")
                sql += "and TenDauSach like N'%" + txt_TenDauSach.Text + "%' ";
            if (txt_NamXB.Text != "" && uti.checkDigit(txt_NamXB.Text) == true)
                sql += "and NamXB=" + Convert.ToInt32(txt_NamXB.Text) + " ";
            if (comb_TinhTrang.SelectedIndex != 0)
                sql += "and TinhTrang=N'" + comb_TinhTrang.SelectedItem.ToString() + "' ";
            if (comb_TrangThai.SelectedIndex != 0)
                sql += "and TrangThai =N'" + comb_TrangThai.SelectedItem.ToString() + "' ";
            sql += "group by a.MaDauSach,TenDauSach,TenTacGia, TenLoaiSach, NamXB, TenNXB";
            return sql;
        }
        private void btn_TimKiem_Click(object sender, EventArgs e)
        {
            dgv_HienThi.DataSource = conn.GetDataTable(getSql());
        }

        private void btn_XemChiTiet_Click(object sender, EventArgs e)
        {
            if (dgv_HienThi.Rows.Count > 1)
            {
                int dong = dgv_HienThi.CurrentCell.RowIndex;
                FrmXemCTSach frm = new FrmXemCTSach();
                frm.madausach = dgv_HienThi.Rows[dong].Cells["MaDauSach"].Value.ToString();
                frm.ShowDialog();
            }
            else
                MessageBox.Show("Không có dữ liệu");

        }
        //Thêm số thứ tự dòng vào RowHeader của DataGridV
        private void dataGridView_HienThi_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_HienThi);
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_ExportExcel_Click(object sender, EventArgs e)
        {
            exp.exportExcel_2(dgv_HienThi,dtb,1);
        }

        private void dgv_HienThi_DoubleClick(object sender, EventArgs e)
        {
            btn_XemChiTiet_Click(sender, e);
        }
    }
}
